package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.MedicalHistory

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.SizeUtils
import com.google.android.flexbox.FlexboxLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityFangAnBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.PrescribingBean
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_fang_an.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.textView
import org.jetbrains.anko.verticalPadding

class FangAnActivity : BaseActivity() {
    private val id by lazy { intent.getStringExtra("id") }

    val binding by lazy { getDataBinding<ActivityFangAnBinding>(R.layout.activity_fang_an) }
    override fun onCreatRootView(): View? {
       return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        titleText.text="方案"
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }

        observe {
            postApi<Request<PrescribingBean>>(hashMapOf("action" to "prescribing",
                    "token" to DoctorInfo.token,"id" to id),
                    Consumer {
                        it.dispose {
                            binding.bean = it
                            initViewFromData()
                        }
                    },context = this)
        }
    }

    private fun initViewFromData(){
        val pres = binding.bean?.prescription?.split("，")
        faa_flexbox.apply {
            val lp = FlexboxLayout.LayoutParams(-1,-1).apply {
                flexBasisPercent = 0.5f
            }
            pres?.forEach {
                textView(it.toString()) {
                    layoutParams = lp
                    verticalPadding = SizeUtils.dp2px(5f)
                }
            }
        }
        tvTangYao.text = "汤药（共${pres?.size}味药）"
    }

}
